<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 操作日志
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除</el-button>
                <el-select v-model="query.key" placeholder="查询条件" class="handle-select mr10" @change="changeKey">
                    <el-option key="1" label="账户" value="account"></el-option>
                    <el-option key="2" label="ip" value="ipAddress"></el-option>
                    <el-option key="3" label="URL" value="url"></el-option>
                </el-select>
                <el-input v-model="query.value" placeholder="内容" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="130" align="center"></el-table-column>
                <el-table-column prop="account" label="账户"></el-table-column>
                <el-table-column prop="url" label="请求URL" width="130"></el-table-column>
                <el-table-column prop="ipAddress" label="请求IP"></el-table-column>
                <el-table-column prop="responseTime" label="响应时间"></el-table-column>
                <el-table-column prop="gmtCreate" label="操作时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

    </div>
</template>

<script>
    import api from "../../../api";
    export default {
        name: "operLog",
        data() {
            return {
                query: {
                    log:{
                        beginDate: null,
                        endDate: null,
                        account: null,
                        ipAddress: null,
                        url: null,

                    },
                    pageIndex: 1,
                    pageSize: 8,
                    key:"",
                    value:""
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            changeKey(val){
                this.query.key=val;
                console.log("key="+this.query.key);
            },

            getData() {
                api.getOperLog(this.query.pageIndex,this.query.pageSize,this.query.log).then(res => {
                    this.tableData = res.data.item.records;
                    for (let i = 0; i < this.tableData.length; i++) {
                        this.tableData[i].ipAddress = this.numberToIp(this.tableData[i].ipAddress);
                    }
                    this.pageTotal = res.data.item.total;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.query.log[this.query.key]=this.query.value;
                this.query.pageIndex=1;
                this.getData();
                this.query.log[this.query.key]=null;
            },
            // 删除操作
            handleDelete(index,row) {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                })
                    .then(() => {
                        let array = [];
                        let Id = {};
                        Id.id = row.id;
                        array[0]=Id;
                        api.deleteLog(array).then((resp) => {
                            if(resp.success){
                                this.$message.success("删除成功");
                                this.tableData.splice(index, 1);
                            }else {
                                this.$message.error("删除失败");
                            }
                        });

                    })
                    .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                })
                    .then(() => {
                        let array = [];
                        for (let i = 0; i < this.multipleSelection.length; i++) {
                            let Id = {};
                            Id.id = this.multipleSelection[i].id;
                            array[i] = Id
                        }
                        api.deleteLog(array).then(response => {
                            if(response.success){
                                this.$message.success("删除成功");
                            }else {
                                this.$message.error("删除失败");
                            }
                            this.getData();
                        }).catch((response) => {
                            console.log("error: " + response.message)
                        });
                        this.multipleSelection = [];
                    })
                    .catch(() => {});




            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.$set(this.tableData, this.idx, this.form);
            },
            // 分页导航
            handlePageChange(val) {
                this.query.pageIndex=val;
                this.getData(this.query.log,this.query.pageIndex,this.query.pageSize);
            },
            numberToIp(number) {
			    // return (Math.floor(number/(256*256*256))) + "." +
			    // (Math.floor(number%(256*256*256)/(256*256))) + "." +
			    // (Math.floor(number%(256*256)/256)) + "." +
			    // (Math.floor(number%256));

                //等价上面
                let ip = [];
                for (let i = 0; i < 4; i++) {
                    ip[i] = number & 0xff;
                    number = number >> 8;
                }
                return ip.join(".");
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
